package com.example.camerademoexcercies1;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.camerademoexcercies1.activity.AlbumActivity;
import com.example.camerademoexcercies1.tool.LogTool;
import com.example.camerademoexcercies1.view.CameraSurfaceView;

public class SplitCamera extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {

    /*
    0.聚焦动画
    1.动态调节分辨率
    2.切换录像
    3.相册 附带删除
    4.动态设置曝光，白平衡参数
    5.以MVP方式重构代码
    */

    /*设置全局参数*/
    private static final String TAG = "SplitCamera";
    private Camera camera;
    private CameraSurfaceView cameraSurfaceView;

    private ImageButton imageButton;
    private ImageButton rotateButton;
    //    private TextureView texturePreview;
    private SurfaceView surfaceView;
    private ImageView albumImage;
    private Surface previewSurface;
    private CameraManager cameraManager;
    private CameraCharacteristics cameraCharacteristics;
    private String cameraId;
    private CameraDevice cameraDevice;
    private HandlerThread handlerThread;
    private Handler mBackgroundHandler;
    private CaptureRequest.Builder previewBuilder;
    private CameraCaptureSession previewSession;
    private String rootPath;
    private Integer menuVisible = 0;
    private ImageButton menuButton;
    private ViewPager viewPager;
    private ImageButton flashButton;
    private int textureWidth;
    private int textureHeight;
    private LinearLayout parentLayout;
    private SurfaceHolder holder;

    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        parentLayout = (LinearLayout) inflater.inflate(R.layout.activity_split_camera, null);
        setContentView(parentLayout);
        new InitCameraThread().start();
    }

    //    点击事件
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_options) {
            //  定义两个菜单栏弹出和收回动画
            final TranslateAnimation animationFromUpToDown = new TranslateAnimation(
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
                    TranslateAnimation.RELATIVE_TO_SELF, -1, TranslateAnimation.RELATIVE_TO_SELF, 0);
            final TranslateAnimation animationFromDownToUp = new TranslateAnimation(
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
            animationFromDownToUp.setDuration(450);
            animationFromUpToDown.setDuration(450);
            if (menuVisible == 0) {
                viewPager.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewPager.setVisibility(menuVisible);
                        viewPager.startAnimation(animationFromUpToDown);
                        menuVisible = 4;
                    }
                }, 0);
            } else {
                viewPager.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewPager.setVisibility(menuVisible);
                        viewPager.setAnimation(animationFromDownToUp);
                        menuVisible = 0;
                    }
                }, 0);
            }
        } else if (v.getId() == R.id.btn_simple_album) {
            Intent intent = new Intent().setClass(this, AlbumActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_options) {
            //  定义两个菜单栏弹出和收回动画
            final TranslateAnimation animationFromUpToDown = new TranslateAnimation(
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
                    TranslateAnimation.RELATIVE_TO_SELF, -1, TranslateAnimation.RELATIVE_TO_SELF, 0);
            final TranslateAnimation animationFromDownToUp = new TranslateAnimation(
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
                    TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
            animationFromDownToUp.setDuration(450);
            animationFromUpToDown.setDuration(450);
            if (menuVisible == 0) {
                viewPager.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewPager.setVisibility(menuVisible);
                        viewPager.startAnimation(animationFromUpToDown);
                        menuVisible = 4;
                    }
                }, 0);
            } else {
                viewPager.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewPager.setVisibility(menuVisible);
                        viewPager.setAnimation(animationFromDownToUp);
                        menuVisible = 0;
                    }
                }, 0);

            }
        }
    }

    //    触摸事件
    @Override
    public boolean onTouch(View v, MotionEvent event) {
//
//        if (v.getId() == R.id.simple_texture_preview) {
//            say(event.toString());
//        }
        return true;
    }

    //    相加正常开启检测
    private boolean safeCameraOpen() {
        boolean isOpen = false;
        try {
            releaseCamera();
            camera = Camera.open();
            isOpen = (camera != null);
        } catch (Exception e) {
            Log.e(TAG, "打开相机失败");
            e.printStackTrace();
        }
        return isOpen;
    }

    //    释放相机资源
    private void releaseCamera() {
        if (camera != null) {
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }
    }

    //    相机初始化线程
    private class InitCameraThread extends Thread {
        @Override
        public void run() {
            super.run();
            if (safeCameraOpen()) {
                say("开启摄像头");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        say("开始初始化view并添加到烦扰frameLayout");
                        cameraSurfaceView = new CameraSurfaceView(SplitCamera.this, camera);
                        FrameLayout previewFrame = findViewById(R.id.surface_frame);
                        previewFrame.addView(cameraSurfaceView);
                        say("添加完成");
                    }
                });
            }
        }
    }

    //    打印
    public void say(String word) {
        LogTool.log(word);
    }

    @Override
    protected void onPause() {
        say("-------------SimplifyActivity paused-------------");
        super.onPause();
        releaseCamera();
    }

    @Override
    protected void onResume() {
        say("-------------SimplifyActivity Resumed-------------");
        if (null == camera) {
            if (cameraSurfaceView != null && safeCameraOpen()) {
                cameraSurfaceView.setCamera(camera);
            } else {
                say("cameraSurfaceView为空或者相机开启失败");
            }
        }
        super.onResume();
    }

    @Override
    protected void onStop() {
        say("-------------SimplifyActivity stopped-------------");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        say("-------------SimplifyActivity destroyed-------------");
        super.onDestroy();
    }


}